package javbot.keydata;
import java.awt.AWTException;
import java.awt.BorderLayout;
import java.awt.Robot;
import java.awt.event.KeyEvent;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class KeyDataInputFrame extends JFrame{

	private static final long serialVersionUID = 9203932138712861613L;

	private int[] keyCodes = {
			KeyEvent.VK_A, KeyEvent.VK_B, KeyEvent.VK_C, KeyEvent.VK_D,
			KeyEvent.VK_E, KeyEvent.VK_F, KeyEvent.VK_G, KeyEvent.VK_H,
			KeyEvent.VK_I, KeyEvent.VK_J, KeyEvent.VK_K, KeyEvent.VK_L,
			KeyEvent.VK_M, KeyEvent.VK_N, KeyEvent.VK_O, KeyEvent.VK_P,
			KeyEvent.VK_Q, KeyEvent.VK_R, KeyEvent.VK_S, KeyEvent.VK_T,
			KeyEvent.VK_U, KeyEvent.VK_V, KeyEvent.VK_W, KeyEvent.VK_X,
			KeyEvent.VK_Y, KeyEvent.VK_Z,
			KeyEvent.VK_1, KeyEvent.VK_2, KeyEvent.VK_3, KeyEvent.VK_4,
			KeyEvent.VK_5, KeyEvent.VK_6, KeyEvent.VK_7, KeyEvent.VK_8,
			KeyEvent.VK_9, KeyEvent.VK_0, KeyEvent.VK_MINUS, KeyEvent.VK_EQUALS,
			KeyEvent.VK_BACK_SLASH, 
			KeyEvent.VK_OPEN_BRACKET, KeyEvent.VK_CLOSE_BRACKET,
			KeyEvent.VK_SEMICOLON,KeyEvent.VK_QUOTE, KeyEvent.VK_NUMBER_SIGN, 
			KeyEvent.VK_COMMA, KeyEvent.VK_PERIOD, KeyEvent.VK_SLASH,
			KeyEvent.VK_SPACE
	};
	
	KeyData data;
	KeyInputTestPanel testPanel;
	Robot robot;	
	
	public KeyDataInputFrame() throws AWTException {
		robot = new Robot();
		data = new KeyData();
		testPanel = new KeyInputTestPanel();
		setContentPane(testPanel);
		setUndecorated(true);
		setSize(20,20);
		setVisible(true);	
	}
	

	public KeyData getKeyDataFromKeyboard() throws Exception {
		
		int ascii, keyCode;
		for (int i = 0; i<keyCodes.length;i++) {
			robot.delay(100);
			keyCode = keyCodes[i];
			// Plus SHIFT
			ascii = getKeyAsciiFromKbrd(true, keyCode);
			if (ascii != -1)
				data.setVkCodeOfAscii(ascii, keyCode, true);
			robot.delay(100);
			// No SHIFT			
			ascii = getKeyAsciiFromKbrd(false, keyCode);
			if (ascii != -1)
				data.setVkCodeOfAscii(ascii, keyCode, false);
		}

		System.out.println("Key Data...");
		data.printKeyData();
		System.out.println("Saving Key Data...");
		data.keyDataProps.writeKeyData();

		return data;
	}

	
	private int getKeyAsciiFromKbrd(boolean shift, int keyEvent) {	
		int asciiCode;
		
		try {
			asciiCode = 0;
			testPanel.jTextField.setText("");
			robot.delay(100);
			if (shift) robot.keyPress(KeyEvent.VK_SHIFT);
			robot.keyPress(keyEvent);
			robot.keyRelease(keyEvent);
			if (shift) robot.keyRelease(KeyEvent.VK_SHIFT);
			robot.delay(100);
			char[] text = testPanel.jTextField.getText().toCharArray();
			if (text.length > 0)
				asciiCode = (int) text[0];
			else {
				System.out.println("Skipped Key");
				System.exit(0);
			}
		} catch (IllegalArgumentException e) {
			return -1;
		}

		return asciiCode;
	}
	
	public class KeyInputTestPanel extends JPanel {

		private static final long serialVersionUID = -2910600579216298709L;
		public JTextField jTextField = null;

		/**
		 * This is the default constructor
		 */
		public KeyInputTestPanel() {
			super();
			initialize();
		}

		/**
		 * This method initializes this
		 * 
		 * @return void
		 */
		private void initialize() {
			this.setLayout(new BorderLayout());
			this.setSize(300, 200);
			this.add(getJTextField(), java.awt.BorderLayout.CENTER);
		}

		/**
		 * This method initializes jTextField	
		 * 	
		 * @return javax.swing.JTextField	
		 */
		private JTextField getJTextField() {
			if (jTextField == null) {
				jTextField = new JTextField();
			}
			
			return jTextField;
		}

		

	}	

}
